package testPackage

import "fmt"

//结构体
type Books struct {
	book_id  int
	anthor   string
	subject  string
	title    string
	bodyText string
}

func SecondTestFunc() {

	//定义数组
	var balance = [5]int{1000, 2, 3, 4, 30}
	var i int

	//打印每一个数组元素,数组下标从0开始
	for i = 0; i < len(balance); i++ {
		fmt.Printf("Element[%d] = %d\n", i, balance[i])
	}

	var book1 Books
	var book2 Books

	book1.book_id = 1
	book1.anthor = "anqi"
	book1.subject = "go"
	book1.title = "go语言基础学习"
	book1.bodyText = "go语言真的不错"

	book2.book_id = 2
	book2.anthor = "269056795@qq.com"
	book2.subject = "java"
	book2.title = "java从入门到放弃"
	book2.bodyText = "java其实是真的好"

	printBook(book1)
	printBook(book2)

	book3 := Books{3, "xiaoming", "mysql", "mysql刪库到跑路", "打包跑路咯"}
	printBook(book3)

	fmt.Println("--------------------分割线---------------------------")

	printBookAddress(&book1)
	printBookAddress(&book2)
	printBookAddress(&book2)

	/*
		语言切片
	*/
	slice1 := make([]int, 3, 5)
	printSlice(slice1)

	slice2 := []int{0, 1, 2, 3, 4}
	printSlice(slice2)

	/* 打印子切片从索引1(包含) 到索引4(不包含)*/
	fmt.Println("slice2[1:4]: ==", slice2[1:4])

	/* 默认下限为 0*/
	fmt.Println("slice2[:4]: ==", slice2[:4])

	/* 默认上限为 len(s)*/
	fmt.Println("slice2[1:]: ==", slice2[1:])

	slice2 = append(slice2, 5)
	printSlice(slice2)

	slice2 = append(slice2, 6, 7, 8)
	printSlice(slice2)

	slice3 := make([]int, len(slice2), cap(slice2)*2)

	copy(slice3, slice2)
	printSlice(slice3)

	/*
		len切片<=cap切片<=len数组，切片由三部分组成：指向底层数组的指针、len、cap
	*/

	/*
		range
	*/
	for _, i := range slice3 {
		fmt.Println("i==", i)
	}

	slice3 = append(slice3, 20)
	for i, num := range slice3 {
		fmt.Printf("i==%d, num==%d\n", i, num)
	}

	kvs := map[string]string{"a": "xiao", "b": "ming"}
	printKvs(kvs)
	fmt.Println("--------------------分割线---------------------------")

	kvs["c"] = "jian"
	kvs["d"] = "le"
	printKvs(kvs)

	/* 查看元素在集合中是否存在 */
	vs, ok := kvs["xiaohua"]
	if ok {
		fmt.Println("输出：", vs)
	} else {
		fmt.Println("不存在xiaohua")
	}

	delete(kvs, "c")
	printKvs(kvs)

	fmt.Println("--------------------分割线---------------------------")
	aa, ok := kvs["r"]
	if ok {
		fmt.Println(aa)
	} else {
		kvs["r"] = "range"
		printKvs(kvs)
	}

	/*
		类型转换
	*/
	var a int = 14
	var b int = 5
	var ment float32
	ment = float32(a) / float32(b)
	fmt.Printf("ment的值为：%f\n", ment)
}

//结构体
func printBook(book Books) {
	fmt.Println()
	fmt.Printf("Book book_id : %d\n", book.book_id)
	fmt.Printf("Book anthor : %s\n", book.anthor)
	fmt.Printf("Book subject : %s\n", book.subject)
	fmt.Printf("Book title : %s\n", book.title)
	fmt.Printf("Book bodyText : %s\n", book.bodyText)
}

//结构体指针
func printBookAddress(book *Books) {
	fmt.Println()
	fmt.Printf("Book book_id : %d\n", book.book_id)
	fmt.Printf("Book anthor : %s\n", book.anthor)
	fmt.Printf("Book subject : %s\n", book.subject)
	fmt.Printf("Book title : %s\n", book.title)
	fmt.Printf("Book bodyText : %s\n", book.bodyText)
}

//打印数组
func printSlice(x []int) {
	fmt.Printf("len=%d cap=%d slice=%v\n", len(x), cap(x), x)
}

//打印map
func printKvs(kvs map[string]string) {
	for k, v := range kvs {
		fmt.Printf("%s --> %s\n", k, v)
	}
}

//打印斐波那契数列
func Feibonaqie(n int) int {
	if 2 > n {
		return n
	} else {
		return Feibonaqie(n-2) + Feibonaqie(n-1)
	}
}
